package com.oscarmovie;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

import tt.util.IOUtils;

public class DoubanPerson {
	public static void main(String[] args) throws Exception {
		
		searchDoubanPeople();
		 

	}

	public static void searchDoubanPeople() {
		List<HashMap<Object, Object>> select = Tools.retrieveObjectWithDoubanIds("person");
		int size = select.size();
		int i = 0;
		for (HashMap<Object, Object> hm : select) {
			int personId = ((BigDecimal) hm.get("ID")).intValue();
			int doubanId = ((BigDecimal) hm.get("DOUBAN_ID")).intValue();
			System.out.println("============================================================");
			System.out.println("No. " + (++i)+"/"+ size + ": " + personId + ", " + doubanId);
			HashMap<String, String> searchDoubanInfo = searchSingleDoubanPerson(doubanId);
			Tools.updatePerson(personId, searchDoubanInfo.get("name"));
		}
	}

	private static HashMap<String, String> searchSingleDoubanPerson(int doubanId) {
		HashMap<String, String> hm = new HashMap<String, String>();
		String url = douban(doubanId, "celebrity");
		System.out.println("URL: " + url);
		List<String> readFileAsList = IOUtils.readHttpUrlAsList(url, "UTF-8");
		for (String s : readFileAsList) {
			s = s.trim();
			//if(!s.equals(""))System.out.println(s);
			if (s.indexOf("<h1>") != -1) {
				String pattern = "<h1>";
				int p1 = s.indexOf(pattern);
				p1 += pattern.length();
				int p2 = s.indexOf("</h1>", p1);
				String ss = s.substring(p1, p2);
				hm.put("name", "" + ss);
			} else if (s.indexOf("celebrity/large/") != -1) {
				String pattern = "href=\"";
				int p1 = s.indexOf(pattern);
				p1 += pattern.length();
				int p2 = s.indexOf("\">", p1);
				String ss = s.substring(p1, p2);
				hm.put("img", "" + ss);
				break;
			}
		}
		return hm;
	}

	/**
	 * 1-b Person Douban ID
	 */
	public static void searchPersonDoubanIds() {
		List<HashMap<Object, Object>> select = Tools.retrieveObjectWithoutDoubanId("person");
		System.out.println(select.size());
		int i = 0;
		for (HashMap<Object, Object> hm : select) {
			int personId = ((BigDecimal) hm.get("ID")).intValue();
			String personName = (String) hm.get("NAME");
			System.out.println("No. " + (++i) + ": " + personId + ", " + personName);
			int doubanId = searchDoubanId(personName, "celebrity");
			Tools.updatePersonDoubanId(personId, doubanId);
		}
	}


	private static String douban(int doubanId, String s) {
		return "http://movie.douban.com/" + s + "/" + doubanId+"/";
	}

	public static int searchDoubanId(String key, String ss) {
		int id = 0;
		String url = baiduDouban(key);
		System.out.println(url);
		List<String> readFileAsList = IOUtils.readHttpUrlAsList(url, "GBK");
		for (String s : readFileAsList) {
			String pattern = "http://movie.douban.com/" + ss + "/";
			int p1 = s.indexOf(pattern);
			if (p1 != -1) {
				System.out.println(s);
				p1 += pattern.length();
				int p2 = p1 + 7;
				String i = s.substring(p1, p2);
				try {
					id = Integer.parseInt(i);
				} catch (Exception e) {
					System.out.println("ERR: " + i);
				}
				System.out.println(id);
				break;
			}
		}
		return id;
	}

	private static String baiduDouban(String key) {
		return "http://www.baidu.com/s?wd=豆瓣+" + key.replace(" ", "+") + "&rsv_bp=0&rsv_spt=3&n=2&inputT=12529";
	}
}
